package great.easychat.help.viewModel

import android.annotation.SuppressLint
import android.app.Application
import android.os.Bundle
import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import com.alibaba.fastjson.JSON
import com.google.gson.reflect.TypeToken
import com.pince.frame.mvvm.architecture.BaseViewModel
import com.pince.json.JsonUtil
import great.easychat.help.Constant
import great.easychat.help.bean.*
import great.easychat.help.http.HttpReposity

class SplashViewModel(application: Application, data: Bundle?) : AppBaseViewModel(application, data) {

    /**
     * 配置获取成功
     */
    val hostConfigLiveData: MutableLiveData<ConfigBean> by lazy { MutableLiveData<ConfigBean>() }

    val userInfoLiveData: MutableLiveData<UserInfoBean> by lazy { MutableLiveData<UserInfoBean>() }

    val addUserInfoLiveData: MutableLiveData<Boolean> by lazy { MutableLiveData<Boolean>() }

    /**
     * 获取配置
     */
    @SuppressLint("CheckResult")
    public fun getConfig() {
        getRepo(HttpReposity::class.java).getConfig(Constant.XIAOBAI_APP_KEY, "app_config", "and", "[[\"config_type\", \"=\", \"" + Constant.CONFIG_NAME + "\"]]")
                .subscribe({
                    var result = checkXiaobaiObject(it)
                    if (result.first == 0) {
                        var configBean = JSON.parseObject(result.second, ConfigBean::class.java)
                        hostConfigLiveData.value = configBean
                    }
                }, {
                    it.printStackTrace()
                })
    }

    @SuppressLint("CheckResult")
    public fun getUserInfo(device_id: String) {
        getRepo(HttpReposity::class.java).getUserInfo(Constant.XIAOBAI_APP_KEY, Constant.USER_DATA_NAME, "and", "[[\"device_id\", \"=\", \"" + device_id + "\"]]").subscribe({
            var result = checkXiaobaiObject(it)
            if (result.first == 0) {
                var userInfoBean = JSON.parseObject(result.second, UserInfoBean::class.java)
                userInfoLiveData.value = userInfoBean
            } else if (result.first == 3) {
                // 状态操作码，0成功；1开发类错误，获取失败(模型不存在)；2应用层规则拦截，拦截后可以小白开放平台查看日记；3查无数据
                // http://hd215.api.yesapi.cn/docs-api-App.Table.FreeFindOne.html
                addUserInfo(UserInfoBean(device_id, "普通用户", "", "", Constant.CONFIG_NAME))
            }
        }, {
            it.printStackTrace()
        })
    }

    @SuppressLint("CheckResult")
    public fun addUserInfo(userInfoBean: UserInfoBean) {
        getRepo(HttpReposity::class.java).addDataItemCheck(Constant.XIAOBAI_APP_KEY, Constant.USER_DATA_NAME, "device_id", JSON.toJSONString(userInfoBean))
                .subscribe({
                    var result = checkXiaobai(it)
                    if (result == 0) {
                        addUserInfoLiveData.value = true
                    } else {
                        addUserInfoLiveData.value = false
                    }
                }, {
                    it.printStackTrace()
                })
    }

}
